package lab3;

import lab3.Course;
import lab3.CourseList;
import lab3.Student;

/**
 * This class provides the basic functionality for storage and retrieval of the
 * Course Object from a list.
 * 
 * @author A.J. Lattanze, CMU
 * @version 1.2, 2011-Feb-24
 */

/*
 * Modification Log **********************************************************
 * 
 * v1.2, R. Champagne, 2011-Feb-24 - Various refactorings, conversion of
 * comments to javadoc format.
 * 
 * v1.1, R. Champagne, 2002-Jun-19 - Adapted for use at ETS.
 * 
 * v1.0, A.J. Lattanze, 12/29/99 - Original version.
 * ***************************************************************************
 */

public class CourseList extends List {

	public CourseList() {
		super();
	}

	/**
	 * Adds a new course to the list. All the issues of casting are taken care
	 * of within this class.
	 * 
	 * @param courseObject
	 */
	public void addCourse(Course courseObject) {
		appendItemToList((Object) courseObject);
	}

	/**
	 * @return the course pointed at the current position pointed to by the
	 *         internal list pointer of the internal list. Subsequent calls will
	 *         return the next Course object in the list. A null object is
	 *         returned if list is empty or the end of list has been reached.
	 */
	public Course getNextCourse() {
		return (Course) getItemFromList();
	}

	/**
	 * Determines whether the course is currently in the list. Note that this
	 * method assumes that all courses have different identification numbers.
	 * 
	 * @param courseObject
	 * @return true if the course is in the list, false otherwise.
	 */
	public boolean isCourseInList(Course courseObject) {
		Course currentObject;
		boolean done = false;
		boolean result = false;

		goToFrontOfList();

		while (!done) {
			currentObject = getNextCourse();

			if (currentObject == null) {
				done = true;
			} else {
				if (courseObject.getCourseID()
						.compareToIgnoreCase(currentObject.getCourseID()) == 0) {
					result = true;
				}
			}
		}

		return (result);

	}

	/**
	 * Finds a Course in a list using the CourseID as the search key. Note that
	 * this method assumes that all courses have different identification
	 * numbers.
	 * 
	 * @param courseID
	 * @param section
	 * @return course if found, null otherwise.
	 */
	public Course findCourseByIDAndSection(String courseID, String section) {
		Course currentObject = null;
		boolean done = false;
		boolean found = false;

		goToFrontOfList();

		while (!done) {
			currentObject = getNextCourse();

			if (currentObject == null) {
				done = true;
			} else {
				if ((currentObject.getCourseID().compareToIgnoreCase(courseID) == 0)
						&& (currentObject.getSection().compareToIgnoreCase(section) == 0)) {
					found = true;
					done = true;
				}
			}
		}

		if (found) {
			return (currentObject);
		} else {
			return (null);
		}
	}
	
	public Course findCourseByID(String CourseID) {

		Course CurrentObject = null;
		boolean Done = false;
		boolean Found = false;

		goToFrontOfList();

		while (!Done) {

			CurrentObject = getNextCourse();

			if (CurrentObject == null) {

				Done = true;

			} else {

				if ((CurrentObject.getCourseID().compareToIgnoreCase(CourseID) == 0)) {
					Found = true;
					Done = true;

				} // if

			} // if

		} // while

		if (Found) {

			return (CurrentObject);

		} else {

			return (null);

		} // if

	} // FindCourseByID
	
	/**
	 * If a student is provided, findCourseByID will autofind the first 
	 * eligible course or return null
	 * @param CourseID
	 * @param StudentObject
	 * @return Course if one is available with the course ID otherwise null
	 */
	public Course findCourseByID(String CourseID, Student StudentObject) {

		Course CurrentObject = null;
		CourseList StudentCourseList =  StudentObject.getCoursesTakenList();
		boolean Done = false;
		boolean Found = false;

		goToFrontOfList();
		StudentCourseList.goToFrontOfList();
		
		
		while (!Done) {

			CurrentObject = getNextCourse();

			if (CurrentObject == null) {

				Done = true;

			} else {

				if (CurrentObject.alreadyRegistered(StudentObject)){
					Done = true;
				} else {
					if ((CurrentObject.getCourseID().compareToIgnoreCase(CourseID) == 0) && 
							CurrentObject.timeCheckNoMessage(StudentObject)) {
						Found = true;
						Done = true;
					}//if
					
				} // if

			} // if

		} // while

		if (Found) {

			return (CurrentObject);

		} else {

			return (null);

		} // if

	} // FindCourseByID
}